package com.yugao.fintech.antelope.statistic.controller;

import com.yugao.fintech.antelope.base.model.module.tenant.TenantContextHolder;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.statistic.api.constant.StatisticCons;
import com.yugao.fintech.antelope.statistic.handler.AbstractStatisticHandler;
import com.yugao.fintech.antelope.statistic.model.rqrs.StatAccessListReq;
import com.yugao.fintech.antelope.statistic.model.rqrs.StatAccessResp;
import com.yugao.fintech.antelope.statistic.service.StatAccessService;
import com.yugao.fintech.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 统计服务
 */
@Api(tags = "统计访问数据")
@RestController
@RequestMapping("/statistic/access")
public class StatAccessController {
    @Autowired
    private StatAccessService accessLogService;

    @ApiOperation(value = "查询统计访问数据记录")
    @GetMapping("/list")
    @RamResource(action = "statistic:access:query", name = "查询统计访问数据", type = ActionType.READ)
    public Resp<List<StatAccessResp>> listStatAccess(StatAccessListReq req) {
        return Resp.ok(accessLogService.listStatAccess(req));
    }

    /**
     * 删除多少天之前的日志
     *
     * @param beforeDays 天数
     */
    @ApiOperation(value = "删除统计的访问数据")
    @DeleteMapping("/deleteTask")
    public Resp<?> deleteStatAccessTask(@RequestParam(value = "beforeDays") Long beforeDays) {
        return Resp.ok(accessLogService.deleteStatAccess(beforeDays));
    }

    @ApiOperation(value = "统计任务")
    @PostMapping("/statTask")
    public Resp<?> statTask() {
        AbstractStatisticHandler.getHandler(StatisticCons.Type.ACCESS).execTask(TenantContextHolder.get().getTenantId());
        return Resp.ok();
    }
}
